package org.jparrow.framework.helper;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import net.sf.cglib.beans.BeanMap;

import org.jparrow.framework.util.ReflectionUtil;

/**
 * 
 * Bean助手类
 * 1.Bean Map 中存放了Bean类与Bean实例的映射关系
 * 
 * bean容器
 * 
 * @author supda
 * @date 2017-3-25
 *
 */
public final class BeanHelper {

	/**
	 * 定义Bean 映射(用于存放Bean类与Bean实例的映射关系)
	 */
	private static final Map<Class<?>,Object> BEAN_MAP = 
			new HashMap<Class<?>, Object>();
	
	static{
		Set<Class<?>> beanClassSet = ClassHelper.getBeanClassSet();
		for(Class<?> beanClass : beanClassSet ) {
			Object obj = ReflectionUtil.newInstance(beanClass);
			BEAN_MAP.put(beanClass, obj);
		}
		System.out.println("BEAN_MAP："+BEAN_MAP);
	}
	
	/**
	 * 获取Bean映射
	 */
	public static Map<Class<?> ,Object> getBeanMap(){
		return BEAN_MAP;
	}
	
	/**
	 * 设置Bean实例
	 */
	public static void setBean(Class<?> cls, Object obj) {
		BEAN_MAP.put(cls, obj);
	}
	
	/**
	 * 获取Bean实例
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(Class<?> cls){
		if(!BEAN_MAP.containsKey(cls)) {
			throw new RuntimeException("can not get bean by class : " +cls);
		}
		return (T) BEAN_MAP.get(cls);
	}
}
